/* 
 * File:   estat_main.c
 * Author: oddi
 *
 * Created on January 20, 2010, 11:38 AM
 */

#include "inc/core/global_definitions.h"
#include "stdio.h"
#include "inc/core/ucgrid3d.h"

#define NPTS    2047

#define TX  (1.5)
#define SIGMA sqrt(BOLTZMAN_CONST * TX / ELECTRON_MASS)
#define E0 (0.03)
#define B0  (25)
#define WCE (QMC*B0)
#define K0  (WCE/SIGMA)
#define N  (10e6)

#define NX (16*16)
#define NY (16*16)
#define NZ (16*16)


void mirror_b_field(time t, floatdig pos, floatdig* field) {
    if (field != NULL) {
        floatdig sin2 = pow(sin(PI*pos/L),2);
        *field = B0*sin2;
    }
}

void uniform_e_field(time t, floatdig pos, floatdig* field) {
    if (field != NULL) {
        floatdig Dz = pos - PULSECENTRE_ZPOS;
        floatdig DtI = t - (PULSEDELAY);
        floatdig DtD = t - (PULSEDELAY + PULSEINCTIME);
        floatdig Az = exp(- pow((Dz / PULSEWIDTH / 2),2));
        floatdig AtI = pow(DtI / PULSEINCTIME / 0.1, 2);
        floatdig AtD = pow(DtD / PULSEDELTIME / 0.4, 2);
        floatdig osc = sin(WCE * t - K0*pos);
        if ((DtI >= 0) && (DtI <= PULSEINCTIME)) {
            *field = E0 * Az * (1 - exp(-AtI)) * osc;
        } else
            if ((DtD >= 0) && (DtD <= PULSEDURATION)) {
            *field = E0 * Az* (1/(1+AtD/3)) * osc;
        } else {
            *field = 0;
        }
    }
}

int main(int argc, char* argv[])
{
    particles_set set;
    ucgrid3d* grid3d;
    particle_initialize_particle_set(&set, N, ELECTRON_CHARGE, ELECTRON_MASS);
    grid3d = ucgrid3d_init_grid(NX, NY, NZ, CELLSIZE, CELLSIZE, CELLSIZE);

    particle_distrubute_particles_set_pos_uniform(&set, X, CELLSIZE, CELLSIZE*(NX - 1));
    particle_distrubute_particles_set_pos_uniform(&set, Y, CELLSIZE, CELLSIZE*(NY - 1));
    particle_distrubute_particles_set_pos_uniform(&set, Z, CELLSIZE, CELLSIZE*(NZ - 1));

    particle_distribute_particle_set_beam(&set, 0, VX);
    particle_distribute_particle_set_beam(&set, 0, VY);
    particle_distribute_particle_set_beam(&set, 0, VZ);

    particle_distribute_particle_set_speed_maxwell(&set, SIGMA, VX);
    particle_distribute_particle_set_speed_maxwell(&set, SIGMA, VY);
    particle_distribute_particle_set_speed_maxwell(&set, SIGMA, VZ);

    particle_dump_particles_speed_into_file(&set, HOMEDIR, "speed.dat", 0 , -1);
    //ucgrid3d_weight_particles_rho(&set, grid3d);
    //ucgrid3d_weight_particles_current(&set, grid3d);
    //ucgrid3d_dump_rho_into_file(grid3d, HOMEDIR, "rho.d3d");
    //ucgrid3d_dump_current_into_file(grid3d, HOMEDIR, "current.d3d");
    particle_clear_particle_set(&set);
    ucgrid3d_free_grid(grid3d);
    return 0;
}
